
/* function getData() {
  setTimeout(function() {
    var name = 'lisa'
    return name
  }, 1000)
}
console.log(getData()) */

/* function getData(callback) {
  setTimeout(function() {
    var name = 'lisa'
    callback(name)
  }, 1000)
}
getData(function(name) {
  console.log(name)
}) */

// Promise来处理异步
/* var p = new Promise((resolve, reject) => {
  setTimeout(() => {
    var name = 'lisa'
    if(Math.random() < 0.7) {
      resolve(name)
    } else {
      reject('错误信息')
    }
  }, 1000)
})
p.then(data => {
  console.log(data)
}).catch(error => {
  console.log(error)
}) */

// async await
/* async function test() {
  return 'hello nodejs'
}
// console.log(test()) // Promise { 'hello nodejs' }
console.log(await test()) // 错误写法 */

/* async function test() {
  return 'hello nodejs'
}
async function main() {
  var data = await test(); // 获取异步方法里面的数据
  console.log(data)
}
main() */

async function test() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      var name = 'lisa'
      if(Math.random() < 0.7) {
        resolve(name)
      } else {
        reject('错误信息')
      }
    }, 1000)
  })
}
async function main() {
  try {
    var data = await test(); // 获取异步方法里面的数据
    console.log(data)
  } catch (error) {
    console.log(error)
  }
 
}
main()

